package com.zp.thread;
/**
 * 当访问某个对象的synchronized方法是static时，表示将该对象所对应的class对象上锁；
 * 所以结果是先打印完hello，再打印完world；
 */
public class SynchronizedTest3 {
	public static void main(String[] args) {
		//第一个对象：Example类的
		Examples examples = new Examples();
		SyThread1 t1 = new SyThread1(examples);
		//第二个对象：Example类的
		examples = new Examples();
		SyThread2 t2 = new SyThread2(examples);
		
		t1.start();
		t2.start();
	}
}

class Examples{
	public synchronized static void excute1(){
		for(int i=0;i<20;i++){
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("hello"+i);
		}
	}
	public synchronized static void excute2(){
		for(int i=0;i<20;i++){
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("wrold"+i);
		}
	}
}

class SyThread1 extends Thread{
	
	private Examples examples;
	
	public SyThread1(Examples examples){
		this.examples = examples;
	}
	
	@Override
	public void run() {
		this.examples.excute1();
	}
}

class SyThread2 extends Thread{
	
	private Examples examples;
	
	public SyThread2(Examples examples){
		this.examples = examples;
	}
	
	@Override
	public void run() {
		this.examples.excute2();
	}
}


